package www.openteam.com.base;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.Time;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import butterknife.ButterKnife;
import www.openteam.com.OTApplication;
import www.openteam.com.R;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.DetailGather;
import www.openteam.com.bean.QueryCondition;
import www.openteam.com.mvp.IPresenter;
import www.openteam.com.mvp.IView;
import www.openteam.com.tool.ToastUtil;

/**
 * 没有查询条件的BaseFragment
 * Created by kylin on 2017/7/24.
 */

public abstract class OtherBaseFragment<P extends IPresenter> extends Fragment
        implements
        SwipeRefreshLayout.OnRefreshListener,
        IView, View.OnClickListener {

    protected View view;
    public SharedPreferences mySetting = OTApplication.getmContext()
            .getSharedPreferences("mySetting", Context.MODE_PRIVATE);
    public SharedPreferences.Editor editor = mySetting.edit();
    protected P mPresenter;
    public static final int REQUEST_COUNT = 10;
    public DetailGather detailGather;   // 详细信息的枚举类
    public String[] detail;
    public SwipeRefreshLayout mSwipeRefresh;  //刷新布局
    public RecyclerView mRecyclerView;


    public final int EDIT_TYPE = 0x123;
    public final int UPDATE_TYPE = 0x124;
    private int year;
    private int dayOfMonth;
    private int monthOfYear;
    private int syear;
    private int smonthOfYear;
    private int sdayOfMonth;
    public long currentMilli;
    public long startMilli;
    public long endMilli;
    private Context mContext;
    public ProgressDialog mProgressDialog;
    private int total_h;

    public boolean arrow_statue = true;
    public Animation animation;
    public float disY;
    public float a_disY;
    public float disMul;
    public GestureDetectorCompat gestureDetectorCompat;
    private LinearLayoutManager linearLayoutManager;

    protected ArrayList<CustomType.DataBean> payTypesclass = new ArrayList<CustomType.DataBean>();
    protected ArrayList<CustomType.DataBean> moneyTypesclass = new ArrayList<CustomType.DataBean>();
    protected ArrayList<CustomType.DataBean> sendTypesclass = new ArrayList<CustomType.DataBean>();
    protected ArrayList<String> payTypes;
    protected ArrayList<String> moneryTypes;
    protected ArrayList<String> sendTypes;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        getView(inflater, container);
        view = inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, view);
        mPresenter = loadPresenter();
        initCommonData();
        initView();
        initListener();
        initData();
        initCalendar();

        return view;

    }


    private void initCommonData() {
        if (mPresenter != null)
            mPresenter.attachView(this);
        payTypesclass.add(new CustomType.DataBean("01", "20号月结，月结60天，23号收款"));
        payTypesclass.add(new CustomType.DataBean("02", "货到，验收后现金付款"));
        payTypesclass.add(new CustomType.DataBean("03", "25号月结，月结30天(寄付)"));
        payTypesclass.add(new CustomType.DataBean("04", "25号月结，月结60天"));
        payTypesclass.add(new CustomType.DataBean("05", "25号月结，月结90天"));
        payTypesclass.add(new CustomType.DataBean("06", "月底月结，月结60天，月底收款"));
        payTypesclass.add(new CustomType.DataBean("07", "23号月结，月结90天，23号收款"));
        payTypesclass.add(new CustomType.DataBean("08", "25号月结，月结105天，15号收款"));
        payTypesclass.add(new CustomType.DataBean("09", "25号月结，月结60天，月底收款"));
        payTypesclass.add(new CustomType.DataBean("10", "23号月结，月结150天，14号收款"));
        payTypes = new ArrayList<>();
        for (int i = 0; i < payTypesclass.size(); i++) {
            payTypes.add(payTypesclass.get(i).getName());
        }
        moneyTypesclass.add(new CustomType.DataBean("01", "RMB  人民币"));
        moneyTypesclass.add(new CustomType.DataBean("02", "HKD  港币"));
        moneyTypesclass.add(new CustomType.DataBean("03", "USD  美金"));
        moneyTypesclass.add(new CustomType.DataBean("04", "RMB  RMB"));
        moneyTypesclass.add(new CustomType.DataBean("06", "NTD  台币"));
        moneyTypesclass.add(new CustomType.DataBean("07", "EUR  欧元"));
        moneryTypes = new ArrayList<>();
        for (int i = 0; i < moneyTypesclass.size(); i++) {
            moneryTypes.add(moneyTypesclass.get(i).getName());
        }
        sendTypesclass.add(new CustomType.DataBean("01", "我厂送货"));
        sendTypesclass.add(new CustomType.DataBean("02", "货送我厂"));
        sendTypesclass.add(new CustomType.DataBean("03", "快递(寄付)"));
        sendTypesclass.add(new CustomType.DataBean("04", "快递(到付)"));
        sendTypesclass.add(new CustomType.DataBean("05", "委托货运公司送货"));
        sendTypesclass.add(new CustomType.DataBean("06", "委托货运公司上门提货"));
        sendTypesclass.add(new CustomType.DataBean("07", "客户上门自提"));
        sendTypesclass.add(new CustomType.DataBean("08", "我厂上门自提"));
        sendTypes = new ArrayList<>();
        for (int i = 0; i < sendTypesclass.size(); i++) {
            sendTypes.add(sendTypesclass.get(i).getName());
        }
        mProgressDialog = new ProgressDialog(getContext());
    }

    protected abstract void initData();

    protected abstract void initListener();

    protected abstract void initView();

    protected abstract void otherViewClick(View view);


    protected abstract P loadPresenter();


    protected abstract int getLayoutId();


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                otherViewClick(v);
                break;
        }
    }


    /**
     * 初始化日历时间
     */
    private void initCalendar() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        monthOfYear = calendar.get(Calendar.MONTH);
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        currentMilli = calendar.getTimeInMillis();
        Time time = new Time("GMT+8");
        time.setToNow();

        syear = time.year;
        smonthOfYear = time.month;
        sdayOfMonth = time.monthDay;
    }


//    private long changeMilli(int year, int month, int day) {
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd", Locale.ENGLISH);
//        String str = year + "-" + (month + 1) + "-" + day;
//        long millionSeconds = 0;//毫秒
//        try {
//            millionSeconds = sdf.parse(str).getTime();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
////        Log.d("今天", millionSeconds + "");
//        return millionSeconds;
//    }
//
//
//    public long changeMilli(String str) {
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd", Locale.ENGLISH);
//        long millionSeconds = 0;//毫秒
//        try {
//            millionSeconds = sdf.parse(str).getTime();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
////        Log.d("今天", millionSeconds + "");
//        return millionSeconds;
//    }


    public void initSRView(View view) {


        mSwipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        mSwipeRefresh.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorAccent);
        mSwipeRefresh.setOnRefreshListener(this);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(getActivity());

        mRecyclerView.setLayoutManager(linearLayoutManager);

    }


    public void createProgressDialog(String content) {

        mContext = getContext();
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mContext);
        }
        mProgressDialog.setMessage(content);
        mProgressDialog.show();


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

}
